package com.sense.kukoo.view

import android.Manifest
import android.os.Build
import com.alibaba.android.arouter.launcher.ARouter
import com.sense.kukoo.common.constant.ARouterUrl
import com.sense.kukoo.R
import com.sense.kukoo.common.mvvm.view.BaseMvvmActivity
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.vm.SplashViewModel
import com.sense.kukoo.databinding.AppActivitySplashBinding
import com.sense.kukoo.lib_grant.MyPermissionCallback
import com.sense.kukoo.lib_logger.LoggerManager
import java.util.concurrent.TimeUnit

class SplashActivity : BaseMvvmActivity<SplashViewModel, AppActivitySplashBinding>() {

    private val TAG: String = javaClass.simpleName

    override fun getLayoutId(): Int = R.layout.app_activity_splash

    override fun getViewModelClass(): Class<SplashViewModel> = SplashViewModel::class.java

    override fun initData() {
        mBinding.lifecycleOwner = this
        mBinding.vm = mViewModel
    }

    override fun initView() {
        handDeepLink()
        checkPermission()
    }

    override fun responseVM(action: Int) {
        when (action) {
            SplashViewModel.ACTION_GO_MAIN_PAGE -> {
                ARouter.getInstance()
                    .build(ARouterUrl.ROUTE_MAIN_MODULE_MAIN_ACTIVITY)
                    .navigation()
                finish()
            }
            else -> {}
        }
    }
    fun handDeepLink() {
        val uri = intent.data
        if (uri != null) {
            val path = uri.toString()
            decodeLink(path)
        }
    }
    private fun decodeLink(link: String){
        LoggerManager.d(TAG, "decodeLink link=${link}")
        val waveeTag = "wavee://main/?"
        if (link.startsWith(waveeTag)) {
            val dataString = link.substring(waveeTag.length, link.length)
            val contentidTag = "contentid="
            if(dataString.startsWith(contentidTag)){
                val contentid = dataString.substring(contentidTag.length, dataString.length)
                Repo.link.setContentId(contentid)
            }
        }
    }
    private fun checkPermission() {
        mViewModel.onGetConfigInfo()
    }

}